#include "FocusWidget.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>
FocusWidget::FocusWidget(QWidget *parent) : QWidget(parent),m_nMilliseconds(0)
{
    // 去除标题栏
    this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    initForm();
    initializeLayout();
    setWindowSize(400,700);
}

FocusWidget::~FocusWidget()
{
    if(m_pChart){
        delete m_pChart;
        m_pChart = nullptr;
    }
    if(m_pTitleLbl){
        delete m_pTitleLbl;
        m_pTitleLbl = nullptr;
    }
    if(m_pTargetLbl){
        delete m_pTargetLbl;
        m_pTargetLbl = nullptr;
    }
    if(m_pTimeLbl){
        delete m_pTimeLbl;
        m_pTimeLbl = nullptr;
    }
    if(m_pContntEdit){
        delete m_pContntEdit;
        m_pContntEdit = nullptr;
    }
    if(m_pConfirmBtn){
        delete m_pConfirmBtn;
        m_pConfirmBtn = nullptr;
    }
}

void FocusWidget::setThemeName(const QString &name)
{
    m_strThemeName = !name.isEmpty()?name:"任务";
    m_pTitleLbl->setText(m_strThemeName);
    update();
}

void FocusWidget::addEvent(const QString &event)
{
    if(m_pChart && !event.isEmpty()){
        m_pChart->addEvent(event);
    }
}

void FocusWidget::addEvents(const QVector<QString> &events)
{
    if(m_pChart && !events.isEmpty()){
        m_pChart->addEvents(events);
    }
}

void FocusWidget::setCountdown(int milliseconds)
{
    if(milliseconds>0){
        m_nMilliseconds = milliseconds;
    }
    QString strTarget = QString("专注时长:%1分钟").arg(m_nMilliseconds/(1000*60));
    m_pTargetLbl->setText(strTarget);
    update();
}

void FocusWidget::setWindowSize(int width, int height)
{
    if(m_pContntEdit){
        m_pContntEdit->setPlaceholderText("专注过程中有什么收获？");
        // 设置最小和最大尺寸
        m_pContntEdit->setMinimumSize(width, height*0.2);
        m_pContntEdit->setMaximumSize(width*1.5, height*0.3);
    }
    if(m_pChart){
        m_pChart->setWindowSize(width,height*0.45);
    }
    if(m_pConfirmBtn){
        m_pConfirmBtn->setMinimumSize(width*0.3, height*0.08);
    }
    resize(width,height);
}

void FocusWidget::setStartDataTime()
{
    m_startTime = QDateTime::currentDateTime();
}

void FocusWidget::setStopDataTime()
{
    m_stopTime = QDateTime::currentDateTime();
    updateTimeLabel(generateTimeRangeString());
    update();
}


void FocusWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (isDragging) {
        move(window_top_left_point + (event->globalPos() - mouse_startPoint));
    }
}

void FocusWidget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = true;
        mouse_startPoint = event->globalPos();
        window_top_left_point = frameGeometry().topLeft();
    }
}

void FocusWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        isDragging = false;
    }
}

void FocusWidget::initForm()
{

    m_pTitleLbl = new QLabel(this);
    m_pTitleLbl->setText("主题");
    // 设置样式
    m_pTitleLbl->setStyleSheet(
        "QLabel {"
        "   font-family: '幼圆';"           // 设置字体名称
        "   font-size: 25px;"              // 设置字体大小
        "   font-weight: bold;"            // 设置字体加粗
        "   color: #919191;"                  // 设置字体颜色
        "   background-color: #ffffff;"  // 设置背景颜色
        "   padding: 0px;"                // 设置内边距
        "   margin: 10px;"                        // 设置外边距为10px，增加控件之间的间距
        "   border-radius: 5px;"           // 设置圆角边框
        "   text-align: center;"           // 设置文本居中
        "}"
    );
    m_pTargetLbl = new QLabel(this);
    QString strTarget = QString("专注时长:%1分钟").arg(m_nMilliseconds/(1000*60));
    m_pTargetLbl->setText(strTarget);
    m_pTargetLbl->setStyleSheet(
        "QLabel {"
        "   font-family: '幼圆';"           // 设置字体名称
        "   font-size: 18px;"              // 设置字体大小
        "   font-weight: normal;"            // 设置字体加粗
        "   color: #919191;"                  // 设置字体颜色
        "   background-color: #ffffff;"  // 设置背景颜色
        "   padding: 0px;"                // 设置内边距
        "   margin: 10px;"                        // 设置外边距为10px，增加控件之间的间距
        "   border-radius: 5px;"           // 设置圆角边框
        "   text-align: center;"           // 设置文本居中
        "}"
    );
    m_pTimeLbl = new QLabel(this);
    m_pTimeLbl->setText("2024/11/22 14:36-20:20");
    m_pTimeLbl->setStyleSheet(
        "QLabel {"
        "   font-family: '幼圆';"           // 设置字体名称
        "   font-size: 18px;"              // 设置字体大小
        "   font-weight: normal;"            // 设置字体加粗
        "   margin: 10px;"                        // 设置外边距为10px，增加控件之间的间距
        "   color: #919191;"                  // 设置字体颜色
        "   background-color: #ffffff;"  // 设置背景颜色
        "   padding: 0px;"                // 设置内边距
        "   border-radius: 5px;"           // 设置圆角边框
        "   text-align: center;"           // 设置文本居中
        "}"
    );
    m_pContntEdit = new QTextEdit(this);
    // 设置优化后的样式
    m_pContntEdit->setStyleSheet(
        "QTextEdit {"
        "   border: 1px solid #919191;"          // 设置边框颜色为深灰色，并增加边框宽度
        "   border-radius: 10px;"                 // 圆角设置为12px，适中圆滑
        "   padding: 0px;"                        // 设置内边距为8px，让文本与边框之间有适当的空间
        "   margin: 10px;"                        // 设置外边距为10px，增加控件之间的间距
        "   background-color: #f9f9f9;"          // 设置背景色为浅灰色
        "   color: #333333;"                      // 设置文本颜色为深灰色，提升可读性
        "   font-size: 16px;"                     // 设置字体大小为14px
        "   font-family: '幼圆';"                // 设置字体为Arial
        "   selection-background-color: #6fa3ef;" // 设置选中文本的背景颜色
        "   selection-color: white;"              // 设置选中文本的颜色为白色
        "}"
    );
    m_pContntEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    m_pChart = new TimelineChart(this);
    m_pConfirmBtn = new QPushButton("确定",this);
    m_pConfirmBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: rgb(87, 205, 255);"
        "   color: white;"
        "   border-radius: 25px;"
        "   padding: 10px 20px;"
        "   font-size: 16px;"
        "}"
        "QPushButton:hover {"
        "   background-color: lightblue;"     // 鼠标悬停时背景颜色变为浅蓝色
        "}"
        "QPushButton:pressed {"
        "   background-color: #3CC5FF;"      // 按下时背景颜色变为深蓝色
        "   padding: 12px 22px;"               // 按钮按下时的内边距
        "}"
    );
    connect(m_pConfirmBtn,&QPushButton::clicked,this,&FocusWidget::onConfirmBtnClick);
}

void FocusWidget::initializeLayout()
{
    // 设置窗口为无边框和透明背景
    this->setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 创建主布局
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainWidget = new QWidget(this);
    mainWidget->setObjectName("FocusWidget");
     // 设置整个TimelineChart控件的边框
    mainWidget->setStyleSheet("#FocusWidget { "
                         "border: none; "
                         "padding: 10px; "
                         "background-color: #ffffff; "
                         "border-radius: 15px;"           // 设置圆角边框
                         "}");
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setSpacing(0);
    layout->setContentsMargins(10, 10, 10, 10);

    layout->addWidget(m_pTitleLbl,0,Qt::AlignLeft|Qt::AlignTop);
    layout->addWidget(m_pTargetLbl,0,Qt::AlignLeft);
    layout->addWidget(m_pTimeLbl,0,Qt::AlignLeft);
    QLabel* tempLabel = new QLabel("感想",this);
    tempLabel->setStyleSheet(
        "QLabel {"
        "   font-family: '幼圆';"           // 设置字体名称
        "   font-size: 18px;"              // 设置字体大小
        "   font-weight: normal;"            // 设置字体加粗
        "   color: #919191;"                  // 设置字体颜色
        "   background-color: #ffffff;"  // 设置背景颜色
        "   padding: 0px;"                // 设置内边距
        "   margin: 10px;"                        // 设置外边距为10px，增加控件之间的间距
        "   border-radius: 5px;"           // 设置圆角边框
        "   text-align: center;"           // 设置文本居中
        "}"
    );
    layout->addWidget(tempLabel,0,Qt::AlignLeft);
    layout->addWidget(m_pContntEdit,0,Qt::AlignLeft);
    layout->addWidget(m_pChart,0,Qt::AlignLeft);
    layout->addWidget(m_pConfirmBtn,0,Qt::AlignHCenter|Qt::AlignBottom);

    // 设置布局到主窗口
    mainWidget->setLayout(layout);
    mainLayout->addWidget(mainWidget, 0, Qt::AlignCenter);
    setLayout(mainLayout);

    // 创建阴影效果
    QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setBlurRadius(10);  // 设置模糊半径
    shadowEffect->setOffset(0, 3);    // 设置阴影偏移量
    shadowEffect->setColor(QColor(0, 0, 0, 128));  // 设置阴影颜色，半透明黑色

    // 将阴影效果应用到 mainWidget 上
    mainWidget->setGraphicsEffect(shadowEffect);
    setLayout(mainLayout);

}

QString FocusWidget::generateTimeRangeString()
{
    // 将开始时间和结束时间格式化为 "yyyy/MM/dd HH:mm"
    QString startStr = m_startTime.toString("yyyy/MM/dd HH:mm");
    QString stopStr = m_stopTime.toString("HH:mm");

    // 拼接并返回
    return startStr + "-" + stopStr;
}

void FocusWidget::updateTimeLabel(const QString &str)
{
    m_pTimeLbl->setText(str);
}

void FocusWidget::onConfirmBtnClick()
{
    close();
}
